package net.fdukedom.epicurus.pm.extensions.menu;

import net.fdukedom.epicurus.core.extensions.menu.MenuExtensionHandler;
import net.fdukedom.epicurus.resource.ResourceDispatcher;
import net.fdukedom.epicurus.pm.domain.util.PrivateMessageHelper;
import net.fdukedom.epicurus.domain.entity.User;

import java.util.HashMap;
import java.util.Map;
import java.text.MessageFormat;
import java.text.FieldPosition;

/**
 * Adds Private messages item to menu
 *
 * @author Dmitry Y. Kochelaev
 */
public class MessagesExtension extends MenuExtensionHandler {

    private ResourceDispatcher resourceDispatcher = ResourceDispatcher.getInstance();

    private PrivateMessageHelper privateMessageHelper = PrivateMessageHelper.getInstance();

    private MessageFormat pmTitle = new MessageFormat(resourceDispatcher.getBundledValue("bundles.pm.Extensions", "pm.menu.title"));

    private String pmLink = resourceDispatcher.getBundledValue("bundles.pm.Extensions", "pm.menu.actionLink");

    public Map<String, Object> execute(Map<String, Object> parameters) {
        Map<String, Object> result = new HashMap<String, Object>();
        Boolean isVisible = ((Boolean) parameters.get("isLoggedIn"));
        result.put(IS_EXTENSION_VISIBLE, isVisible);
        if (isVisible) {
            int count = privateMessageHelper.getNumberOfNotRead((User) parameters.get("user"));
            result.put(EXTENSION_TITLE, pmTitle.format(new Object[]{count}, new StringBuffer(), new FieldPosition(0)).toString());
            result.put(EXTENSION_ACTION, pmLink);
        }
        return result;
    }

    public String getPresentation() {
        return "";
    }
}
